package com.example.other.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.other.domain.Checkitem;
import com.example.other.domain.Checkitemdetailed;
import com.example.other.domain.Cidetailedreport;
import com.example.other.domain.Overallresult;
import com.example.other.domain.dto.CheckItemReportDto;
import com.example.other.domain.dto.ReportDto;
import com.example.other.mapper.CheckitemMapper;
import com.example.other.mapper.CheckitemdetailedMapper;
import com.example.other.mapper.CidetailedreportMapper;
import com.example.other.mapper.OverallresultMapper;
import com.example.other.service.CheckitemdetailedService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Limhuang
 * @description 针对表【checkitemdetailed】的数据库操作Service实现
 * @createDate 2024-07-03 10:15:00
 */
@Service
public class CheckitemdetailedServiceImpl
        extends ServiceImpl<CheckitemdetailedMapper, Checkitemdetailed>
        implements CheckitemdetailedService {

    @Autowired
    private CidetailedreportMapper cidetaileddMapper;

    @Autowired
    private OverallresultMapper overallresultMapper;

    @Autowired
    private CheckitemMapper checkitemMapper;

    @Override
    public ReportDto getReportByOrderId(Integer id) {
        ReportDto reportDto = new ReportDto();

        // 1. 根据订单ID获取总检报告结果
        reportDto.setResults(overallresultMapper.selectList(
                new QueryWrapper<Overallresult>().eq("orderId", id)));

        // 2. 根据订单ID获取所有检查项明细报告
        List<Cidetailedreport> reportItemList = cidetaileddMapper.selectList(
                new QueryWrapper<Cidetailedreport>().eq("orderId", id)
        );

        // 3. 按 ciId 分组检查项明细报告
        Map<Integer, List<Cidetailedreport>> groupedReports = reportItemList.stream()
                .collect(Collectors.groupingBy(Cidetailedreport::getCiid));

        // 4. 构建 CheckItemReportDto 列表
        List<CheckItemReportDto> checkItemReportDtoList = new ArrayList<>();
        for (Map.Entry<Integer, List<Cidetailedreport>> entry : groupedReports.entrySet()) {
            // 4.1 获取ciId和对应的检查项明细报告列表
            Integer ciId = entry.getKey();
            List<Cidetailedreport> reports = entry.getValue();

            // 4.2 根据ciId查询检查项信息
            Checkitem checkitem = checkitemMapper.selectById(ciId);

            // 4.3 创建 CheckItemReportDto 对象并设置属性
            CheckItemReportDto checkItemReportDto = new CheckItemReportDto();
            BeanUtils.copyProperties(checkitem, checkItemReportDto);
            checkItemReportDto.setItems(reports);

            // 4.4 将构建好的CheckItemReportDto对象添加到列表中
            checkItemReportDtoList.add(checkItemReportDto);
        }

        // 5. 将所有CheckItemReportDto对象添加到reportDto中
        reportDto.setItems(checkItemReportDtoList);

        // 返回最终结果
        return reportDto;
    }
}




